package com.reworkssm.mybatisdemo.controller;

import com.reworkssm.mybatisdemo.domain.Account;
import com.reworkssm.mybatisdemo.domain.Login;
import com.reworkssm.mybatisdemo.domain.Product;
import com.reworkssm.mybatisdemo.service.AccountService;
import com.reworkssm.mybatisdemo.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("/account/")
@SessionAttributes({"account","authenticated","myList"})
public class AccountController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private CatalogService catalogService;

    private Account account = new Account();
    private List<Product> myList;
    private boolean authenticated;
/*    private static final String NEW_ACCOUNT = "/account/NewAccountForm";
    private static final String EDIT_ACCOUNT = "/account/EditAccountForm";
    private static final String SIGNON = "/account/SignonForm";*/
/*    private List<String> categories = new ArrayList<>();
    private List<String> languages = new ArrayList<>();*/
    private static final List<String> LANGUAGE_LIST;
    private static final List<String> CATEGORY_LIST;

    static {
        List<String> langList = new ArrayList<>();
        langList.add("ENGLISH");
        langList.add("CHINESE");
        LANGUAGE_LIST = Collections.unmodifiableList(langList);

        List<String> catList = new ArrayList<>();
        catList.add("FISH");
        catList.add("DOGS");
        catList.add("REPTILES");
        catList.add("CATS");
        catList.add("BIRDS");

        CATEGORY_LIST = Collections.unmodifiableList(catList);
    }

/*    @GetMapping("loginForm")
    public String loginForm() {
        return "account/login";
    }*/

    @PostMapping("login")
    public String login(Login accountlogin, Model model) {
        //System.out.println(account.getUsername() + "," + account.getPassword());
        Account account = accountService.login(accountlogin);
        if (account == null) {
            model.addAttribute("msg", "用户名或密码错误");
            return "account/SignonForm";
        } else {
            model.addAttribute("account", account);
            //model.addAttribute("accountList", accountService.getAllAccountInfo());
            return "catalog/Main";
        }
    }

    public AccountController() { }

    public Account getAccount() {
        return account;
    }

    public String getUsername() {
        return account.getUsername();
    }
    /* @Validate(
             required = true,
             on = {"signon", "newAccount", "editAccount"}
     )*/
    public String getPassword() {
        return account.getPassword();
    }

    /*@Validate(
            required = true,
            on = {"signon", "newAccount", "editAccount"}
    )*/

    public void setPassword(String password) {
        account.setPassword(password);
    }


    public List<Product> getMyList() {
        return myList;
    }


    public void setMyList(List<Product> myList) {
        this.myList = myList;
    }

/*    public List<String> getLanguages() {
        return languages;
    }

    @GetMapping
    public List<String> getCategories() {
        return categories;
    }*/
    @GetMapping("newAccountForm")
    public String newAccountForm(Model model){
        model.addAttribute("newAccount",new Account());
        model.addAttribute("LANGUAGE_LIST", LANGUAGE_LIST);
        model.addAttribute("CATEGORY_LIST", CATEGORY_LIST);
        return "account/NewAccountForm";
    }

/*    @GetMapping("SignonForm")
    public String editAccountForm() {
        return "/account/EditAccountForm";
    }*/

    @GetMapping("editAccountForm")
    public String editAccountForm(Account account , Model model) {
        model.addAttribute("account", account);
        model.addAttribute("LANGUAGE_LIST", LANGUAGE_LIST);
        model.addAttribute("CATEGORY_LIST", CATEGORY_LIST);
        return "account/EditAccountForm";
    }
    @PostMapping("editAccount")
    public String editAccount(Account account, String repeatedPassword, Model model) {
        if (account.getPassword() == null || account.getPassword().length() == 0 || repeatedPassword == null || repeatedPassword.length() == 0) {
            String msg = "密码不能为空";
            model.addAttribute("msg", msg);
            return "account/EditAccountForm";
        } else if (!account.getPassword().equals(repeatedPassword)) {
            String msg = "两次密码不一致";
            model.addAttribute("msg", msg);
            return "account/EditAccountForm";
        } else {
            accountService.updateAccount(account);
            account = accountService.getAccount(account.getUsername());
            List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
            boolean authenticated = true;
            model.addAttribute("account", account);
            model.addAttribute("myList", myList);
            model.addAttribute("authenticated", authenticated);
            return "redirect:/catalog/main";
        }
    }

    @GetMapping("SignonForm")
    public String signonForm() {
        return "account/SignonForm";
    }

    @RequestMapping("signon")
    public String signon(@RequestParam("username") String username,@RequestParam("password") String password,Model model) {
        Account account = accountService.getAccount(username, password);
        if (account == null) {
            String msg = "Invalid username or password.  Signon failed.";
            model.addAttribute("msg", msg);
            return "account/SignonForm";
        } else {
            account.setPassword(null);
            List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
            boolean authenticated = true;
            model.addAttribute("account", account);
            model.addAttribute("myList", myList);
            model.addAttribute("authenticated", authenticated);
            return "catalog/Main";
        }
    }


    @GetMapping("signoff")
    public String signoff(Model model) {
        clear();
        model.addAttribute("account", account);
        model.addAttribute("myList", myList);
        model.addAttribute("authenticated", authenticated);
        return "catalog/Main";
    }

    @RequestMapping("addAccount")
    public String addAccount(@ModelAttribute(value = "newAccount") Account account,Model model) {
        {
   /*         AccountService service = new AccountService();
            HttpSession session = request.getSession();*/
            account.setListOption(account.isListOption());
            account.setBannerOption(account.isBannerOption());
/*          String clientCheckcode = request.getParameter("vCode");
            String serverCheckcode = (String)session.getAttribute("checkcode");
            System.out.println(clientCheckcode + " " + serverCheckcode);
            //特别注意想要刷新二维码，不能使用clientCheckcode.equals(serverCheckcode)，clientCheckcode可能为空
            if(username != "" && password != "" && serverCheckcode.equals(clientCheckcode)){*/
            accountService.insertAccount(account);
            account = accountService.getAccount(account.getUsername());
            myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
            authenticated = true;
            model.addAttribute("account",account);
            model.addAttribute("myList",myList);
            model.addAttribute("authenticated",authenticated);
            return "/catalog/Main";
        }
    /*            service.insertAccount(account);
                session.setAttribute("account",account);
                service.rememberUserId(username);*/
/*
                request.getRequestDispatcher(MAIN).forward(request,response);
            }else{
                request.getRequestDispatcher(NEW_ACCOUNT_FORM).forward(request,response);*/
    }



    public boolean isAuthenticated() {
        return authenticated && account != null && account.getUsername() != null;
    }

    public void clear() {
        account = new Account();
        myList = null;
        authenticated = false;
    }
}
